//
//  ProfileScreenTableView.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 7/25/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "ProfileScreenTableView.h"
#import "ProfileTableViewCell.h"
#import "ZooUtil.h"

@implementation ProfileScreenTableView
#define BUTTON_WIDTH 74
#define BUTTON_HEIGHT 62

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        _arSpot = [[NSMutableArray alloc]init];

        _arSpotName = [NSMutableArray arrayWithObjects:@"Sumo BBQ", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", nil];
        _arAddress = [NSMutableArray arrayWithObjects:@"266 Doi Can - Ba Dinh", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", nil];
        _arComment = [NSMutableArray arrayWithObjects:@"Ngon nhwng ma dat x_X", @"Ngon nhwng ma khong ngon", @"Ngon nhwng ma khong ngon", @"Ngon nhwng ma khong ngon", @"Ngon nhwng ma khong ngon", @"Ngon nhwng ma khong ngon", @"Ngon nhwng ma khong ngon", @"Ngon nhwng ma khong ngon", @"Ngon nhwng ma khong ngon", nil];
        _arTime = [NSMutableArray arrayWithObjects:@"5 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", nil]; 
        self.delegate = self;
        self.dataSource = self;
        [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
  

    }
    return self;
}

#pragma mark - Table view data source

- (void)initHeader:(UITableViewCell *)pCell {
    UIImageView *_vAvatarHeaderBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:IMG_HEADER_AVATARPROFILE_BACKGROUND]];
    _vAvatarHeaderBg.frame = CGRectMake(10, 10, 75, 75);
    
    _vAvatarHeader = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 70, 70)];
    [_vAvatarHeader setImage:[UIImage imageNamed:IMG_NOAVATAR_PROFILE]];
    [_vAvatarHeaderBg addSubview:_vAvatarHeader];
    
    _lbNameHeader = [[UILabel alloc]initWithFrame:CGRectMake(90, 12, 220, 20)];
    [_lbNameHeader setBackgroundColor:[UIColor clearColor]];
    [_lbNameHeader setText:@"Kunkka"];
    [_lbNameHeader setFont:[UIFont boldSystemFontOfSize:16]];
    
    _lbAddressHeader = [[UILabel alloc]initWithFrame:CGRectMake(90, 35, 220, 20)];
    [_lbAddressHeader setBackgroundColor:[UIColor clearColor]];
    [_lbAddressHeader setText:@"16 Quan Thổ 3 - Tôn Đức Thắng"];
    [_lbAddressHeader setFont:[UIFont systemFontOfSize:14]];
    [_lbAddressHeader setTextColor:[UIColor grayColor]];
    
    _btnCheckin = [[UIButton alloc]initWithFrame:CGRectMake(10, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
    _vImgCheckin = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
    [_vImgCheckin setImage:[UIImage imageNamed:IMG_ICON_CHECKIN]];
    _lbCheckin = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
    
    _btnLike = [[UIButton alloc]initWithFrame:CGRectMake(10 + BUTTON_WIDTH+1, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
    _vImgLike = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
    [_vImgLike setImage:[UIImage imageNamed:IMG_ICON_LIKE]];
    _lbLike = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
    
    _btnWishList = [[UIButton alloc]initWithFrame:CGRectMake(10 + (BUTTON_WIDTH+1)*2, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
    _vImgWishList = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
    [_vImgWishList setImage:[UIImage imageNamed:IMG_ICON_LIST]];
    _lbWishList = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
    
    _btnFriend = [[UIButton alloc]initWithFrame:CGRectMake(10 + (BUTTON_WIDTH+1)*3, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
    _vImgFriend = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
    [_vImgFriend setImage:[UIImage imageNamed:IMG_ICON_FRIEND]];
    _lbFriend = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
    
    UIView *vHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 160)];
    
    [_btnCheckin setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
    [_btnCheckin addSubview:_vImgCheckin];
    [_lbCheckin setText:@"16"];
    [_btnCheckin addSubview:_lbCheckin];
    [_btnCheckin addTarget:self action:@selector(btnCheckinClicked) forControlEvents:UIControlEventTouchUpInside];
    [_lbCheckin setTextAlignment:UITextAlignmentCenter];
    [_lbCheckin setBackgroundColor:[UIColor clearColor]];
    [_lbCheckin setTextColor:[UIColor whiteColor]];
    
    [_btnLike setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
    [_btnLike addSubview:_vImgLike];
    [_lbLike setText:@"21"];
    [_btnLike addSubview:_lbLike];
    [_btnLike addTarget:self action:@selector(btnLikeClicked) forControlEvents:UIControlEventTouchUpInside];
    [_lbLike setTextAlignment:UITextAlignmentCenter];
    [_lbLike setBackgroundColor:[UIColor clearColor]];
    [_lbLike setTextColor:[UIColor whiteColor]];
    
    [_btnWishList setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
    [_btnWishList addSubview:_vImgWishList];
    [_lbWishList setText:@"07"];
    [_btnWishList addSubview:_lbWishList];
    [_btnWishList addTarget:self action:@selector(btnWishListClicked) forControlEvents:UIControlEventTouchUpInside];
    [_lbWishList setTextAlignment:UITextAlignmentCenter];
    [_lbWishList setBackgroundColor:[UIColor clearColor]];
    [_lbWishList setTextColor:[UIColor whiteColor]];
    
    [_btnFriend setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
    [_btnFriend addSubview:_vImgFriend];
    [_lbFriend setText:@"25"];
    [_btnFriend addSubview:_lbFriend];
    [_btnFriend addTarget:self action:@selector(btnFriendClicked) forControlEvents:UIControlEventTouchUpInside];
    [_lbFriend setTextAlignment:UITextAlignmentCenter];
    [_lbFriend setBackgroundColor:[UIColor clearColor]];
    [_lbFriend setTextColor:[UIColor whiteColor]];
    
    [vHeader addSubview:_vAvatarHeaderBg];
    [vHeader addSubview:_lbNameHeader];
    [vHeader addSubview:_lbAddressHeader];
    [vHeader addSubview:_btnCheckin];
    [vHeader addSubview:_btnLike];
    [vHeader addSubview:_btnWishList];
    [vHeader addSubview:_btnFriend];
    
    [pCell addSubview:vHeader];
    
    [_vAvatarHeaderBg release];
}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    _vAvatarHeader = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 75, 75)];
//    _lbNameHeader = [[UILabel alloc]initWithFrame:CGRectMake(90, 12, 220, 20)];
//    _lbAddressHeader = [[UILabel alloc]initWithFrame:CGRectMake(90, 35, 220, 20)];
//    _btnCheckin = [[UIButton alloc]initWithFrame:CGRectMake(10, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
//    _vImgCheckin = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
//    [_vImgCheckin setImage:[UIImage imageNamed:IMG_ICON_CHECKIN]];
//    _lbCheckin = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
//    
//    _btnLike = [[UIButton alloc]initWithFrame:CGRectMake(10 + BUTTON_WIDTH+1, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
//    _vImgLike = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
//    [_vImgLike setImage:[UIImage imageNamed:IMG_ICON_LIKE]];
//    _lbLike = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
//    
//    _btnWishList = [[UIButton alloc]initWithFrame:CGRectMake(10 + (BUTTON_WIDTH+1)*2, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
//    _vImgWishList = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
//    [_vImgWishList setImage:[UIImage imageNamed:IMG_ICON_LIST]];
//    _lbWishList = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
//    
//    _btnFriend = [[UIButton alloc]initWithFrame:CGRectMake(10 + (BUTTON_WIDTH+1)*3, 92, BUTTON_WIDTH, BUTTON_HEIGHT)];
//    _vImgFriend = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 24, 24)];
//    [_vImgFriend setImage:[UIImage imageNamed:IMG_ICON_FRIEND]];
//    _lbFriend = [[UILabel alloc]initWithFrame:CGRectMake(10, 36, 54, 20)];
//    
//    UIView *vHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 160)];
//    
//    [_btnCheckin setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
//    [_btnCheckin addSubview:_vImgCheckin];
//    [_lbCheckin setText:@"16"];
//    [_btnCheckin addSubview:_lbCheckin];
//    [_btnCheckin addTarget:self action:@selector(btnCheckinClicked) forControlEvents:UIControlEventTouchUpInside];
//    [_lbCheckin setTextAlignment:UITextAlignmentCenter];
//    [_lbCheckin setBackgroundColor:[UIColor clearColor]];
//    [_lbCheckin setTextColor:[UIColor whiteColor]];
//    
//    [_btnLike setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
//    [_btnLike addSubview:_vImgLike];
//    [_lbLike setText:@"21"];
//    [_btnLike addSubview:_lbLike];
//    [_btnLike addTarget:self action:@selector(btnLikeClicked) forControlEvents:UIControlEventTouchUpInside];
//    [_lbLike setTextAlignment:UITextAlignmentCenter];
//    [_lbLike setBackgroundColor:[UIColor clearColor]];
//    [_lbLike setTextColor:[UIColor whiteColor]];
//    
//    [_btnWishList setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
//    [_btnWishList addSubview:_vImgWishList];
//    [_lbWishList setText:@"07"];
//    [_btnWishList addSubview:_lbWishList];
//    [_btnWishList addTarget:self action:@selector(btnWishListClicked) forControlEvents:UIControlEventTouchUpInside];
//    [_lbWishList setTextAlignment:UITextAlignmentCenter];
//    [_lbWishList setBackgroundColor:[UIColor clearColor]];
//    [_lbWishList setTextColor:[UIColor whiteColor]];
//    
//    [_btnFriend setBackgroundImage:[UIImage imageNamed:IMG_BTN_BACKGROUND] forState:UIControlStateNormal];
//    [_btnFriend addSubview:_vImgFriend];
//    [_lbFriend setText:@"25"];
//    [_btnFriend addSubview:_lbFriend];
//    [_btnFriend addTarget:self action:@selector(btnFriendClicked) forControlEvents:UIControlEventTouchUpInside];
//    [_lbFriend setTextAlignment:UITextAlignmentCenter];
//    [_lbFriend setBackgroundColor:[UIColor clearColor]];
//    [_lbFriend setTextColor:[UIColor whiteColor]];
//    
//    [vHeader addSubview:_vAvatarHeader];
//    [vHeader addSubview:_lbNameHeader];
//    [vHeader addSubview:_lbAddressHeader];
//    [vHeader addSubview:_btnCheckin];
//    [vHeader addSubview:_btnLike];
//    [vHeader addSubview:_btnWishList];
//    [vHeader addSubview:_btnFriend];
//    
//    return vHeader;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"MyCell";
    static NSString *CellIdentifier1 = @"MyCell1";
        
    UITableViewCell *cell = nil;
    ProfileTableViewCell *cellView;
    
    if (indexPath.section == 0) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self initHeader:cell];
    } else {
        
        cellView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
            if (cellView == nil){
                cellView = [[ProfileTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier1]; 
                [cellView setSelectionStyle:UITableViewCellSelectionStyleNone];
            } 
            
        NSString *sName = @"Kunkka";
            _arSpotName = [NSMutableArray arrayWithObjects:@"Sumo BBQ", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", nil];
            _arAddress = [NSMutableArray arrayWithObjects:@"266 Doi Can - Ba Dinh", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Tôn Đức Thắng - Đống Đa", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", @"80 Ton Duc Thang - Dong Da", nil];
            _arComment = [NSMutableArray arrayWithObjects:@"Ngon nhung ma dat x_X", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", nil];
            _arTime = [NSMutableArray arrayWithObjects:@"5 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", @"7 ngay truoc", nil]; 

        cellView.lbNameAtSpot.text = [NSString stringWithFormat:@"%@ tại %@", sName, [_arSpotName objectAtIndex:indexPath.row]];
        cellView.lbNameAtSpot.font = [UIFont systemFontOfSize:14];

        cellView.lbAddress.text = [_arAddress objectAtIndex:indexPath.row];
        
        sizeOfComment = [[_arComment objectAtIndex:indexPath.row] sizeWithFont:cellView.lbComment.font constrainedToSize:CGSizeMake(280, 140) lineBreakMode:UILineBreakModeWordWrap];
    
        NSLog(@"%f %f",sizeOfComment.width,sizeOfComment.height);
        cellView.vImgComment.frame = CGRectMake(8, 49, 280, sizeOfComment.height+30);
        cellView.lbComment.frame = CGRectMake(8, 8, 260, sizeOfComment.height+8);
        cellView.vImgFood.frame = CGRectMake(1, 49+sizeOfComment.height+35, 298, 93);
        cellView.lbTime.frame = CGRectMake(1, 49+sizeOfComment.height+35+93, 298, 24);
        [cellView.vImgFood setImage:[UIImage imageNamed:@"img_foodpicture.png"]];
        cellView.lbComment.text = [_arComment objectAtIndex:indexPath.row];
        cellView.lbTime.text = [_arTime objectAtIndex:indexPath.row];
        cellView.vImgAvatar.image = [UIImage imageNamed:IMG_NOAVATAR_PROFILE];
        
        cell = cellView;
    }
    
    return cell;
//    }
//	cellView.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    return cellView;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        // Return the number of rows in the section.
    if (section == 0) {
        return 1;
    }

    _arSpotName = [NSMutableArray arrayWithObjects:@"Sumo BBQ", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", @"Kichi kichi", nil];
    return [_arSpotName count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        // Return the number of sections.
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 160;
    }
    
    
    ProfileTableViewCell *cellView = [[ProfileTableViewCell alloc]init];
    _arComment = [NSMutableArray arrayWithObjects:@"Ngon nhung ma dat x_X", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", @"Ngon nhưng mà khong ngon abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc  abc abc abc abc abc abc abc abc abc abc abc ", nil];
    
    UIFont *font1 = cellView.lbComment.font;
    sizeOfComment = [[_arComment objectAtIndex:indexPath.row] sizeWithFont:font1 constrainedToSize:CGSizeMake(280, 140) lineBreakMode:UILineBreakModeWordWrap];
    
    NSLog(@"%f %f", sizeOfComment.width, sizeOfComment.height);
    
    [cellView release];
    return 217 + sizeOfComment.height;
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    CGFloat sectionHeaderHeight = 160;
//    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
//        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
//        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//    }
//}

-(void)btnCheckinClicked {
    NSLog(@"checkin");
}

-(void)btnLikeClicked {
    NSLog(@"Like");
}

-(void)btnWishListClicked {
    NSLog(@"Wishlist");
}

-(void)btnFriendClicked {
    NSLog(@"Friend");
}
@end
